<?php
/*
Plugin Name: HOT-Topic
Plugin URI: http://www.xys-reader.org/blogs
Description: Let mutil-blog users add post to a same catalog.
Version: 0.10
Author: LIU Xuefeng
Author URI: http://www.xy-reader.og
*/


/*  
	Copyright 2008  LIU Xuefeng  (email : xfliu.math@gmail.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
//require(WPADMININC."install-helper.php");

add_filter('the_content', 'hot_filter');
function hot_filter($content) {

        global $blog;
	$hot_cat_id=27353;
	$hot_cat_id=28119;

        get_currentuserinfo();

        $myblogs = get_userblogs();
        $sys_admin_user=0;
        if ($myblogs) {
                foreach ($myblogs as $b)
                        if($b->id ==2) $sys_admin_user=1;
        }
        if($sys_admin_user ){

    global $id, $post;
    $uid=$post->guid;
    preg_match("/blogs\/([^\/?]*)/",$uid,$matched);
	
    $url="http://www.de-sci.org/blogs/".$matched[1]."/?p=".$post->ID;
    $special_edit="\n<div align=\"right\" >\n<form action=\"http://www.de-sci.org/xys/p_add_product_to_cat_by_link.php\" method=\"post\" target=\"_new\">";

    $special_edit.="<input type=\"hidden\" name=\"parent_cat_id\" value=\"$hot_cat_id\">\n";
    $special_edit.="<input type=\"hidden\" name=\"arc_title\" value=\"".$post->post_title."\">\n";
    $special_edit.="<input type=\"hidden\" name=\"arc_link\" value=\"$url\">\n";
    $special_edit.="<input type=\"hidden\" name=\"arc_author\" value=\"".$post->author_nickname."\">\n";
    $special_edit.="<input type=\"hidden\" name=\"arc_date\" value=\"".$post->post_modified."\">\n";
    $special_edit.="<input type=\"hidden\" name=\"arc_ID\" value=\"".$post->ID."\">\n";
    $special_edit.="<input type=\"submit\" name=\"arc_submit\" value=\"Hide this on top page\">";
    $special_edit.="</form>\n</div>\n\n";

   //$special_edit.=print_r($post,1);
   //$special_edit.=print_r($blog,1);

  }
  if(!is_home()) $content=$special_edit.$content.$special_edit;


	return $content;




}


### Function: Processing Sticky Post
add_action('save_post', 'add_hot_admin_process');
function add_hot_admin_process($post_ID) {

	global $wpdb;

	$shop_id=3;
	require( XYSREADERLIB."/db.lib" );
//	require("/home/xfliu/www/lib/std.lib");

	$post_hot_cat = intval($_POST['hot_cat']);
	$old_hot_cat = intval($_POST['old_hot_cat']);



	$p_id=get_the_guid($post_ID);
	$arc_result = $wpdb->get_results("SELECT post_title, DATE_FORMAT( post_date_gmt + INTERVAL 9 HOUR, '%Y%m%d%k%m' )  as post_date, $wpdb->users.display_name as name FROM $wpdb->posts inner join $wpdb->users on post_author=$wpdb->users.ID WHERE $wpdb->posts.ID=$post_ID");
	$arc_date=$arc_result[0]->post_date;
	$arc_date=substr($arc_date,0,8);

	$title=$arc_result[0]->post_title;
	$title = iconv("utf-8","gbk",$title);

	$author = $arc_result[0]->name;
	$author = iconv("utf-8","gbk",$author);
	
	if($post_hot_cat!=$old_hot_cat){
		if($old_hot_cat){
			product_del($shop_id,$p_id);
			cat_set_product_num_local($shop_id,$old_hot_cat);
		}
		if($post_hot_cat){
			product_add($shop_id, $p_id,$title,$author,$arc_date,$post_ID);	
			product_addto_cat($shop_id,$p_id,$post_hot_cat,$title);	
			cat_set_product_num_local($shop_id,$post_hot_cat);
		}
	}

/*
*/

	
}


### Function: Delete Away Sticky If Post Is Deleted
add_action('delete_post', 'delete_hot_admin_process');
function delete_hot_admin_process($post_ID) {
	global $wpdb;
	//	$wpdb->query("DELETE FROM $wpdb->sticky WHERE sticky_post_id = $post_ID");
}


### Function: Add Sticky To Admin
add_action('dbx_post_sidebar', 'hot_admin');
function hot_admin() {

return;
	global $wpdb,$post;
	require( XYSREADERLIB."/db.lib" );
	require( XYSREADERLIB."/std.lib" );
	$hot_cat_id=27353;
	$hot_cat_id=28119;
	$shop_id=3;
	$in_hot_list=0;

	$old_hot_cat="<input type=hidden value=0 name=\"old_hot_cat\">";

	$edit_post = intval($_GET['post']);

	if($edit_post > 0 ) {
		$cat_list=cat_get_childs($shop_id, $hot_cat_id);
		$cat_list_str="";
		$cat_list_str.="<select name=\"hot_cat\" >";
		if($cat_list) {
			$i=0;
			foreach($cat_list as $cat){
				$selected_str="";
				if( product_cat_exist($shop_id,$cat,$post->guid) ) { $selected_str=" selected "; $in_hot_list=1; 
				$old_hot_cat="<input type=hidden value=$cat name=\"old_hot_cat\">";
}
				$cat_list_str.="<option value=\"$cat\" $selected_str >".cat_get_alias($shop_id,$cat)."\n";
				
			}
		}
		$selected_str="";
		if(! $in_hot_list ) $selected_str=" selected ";
		$cat_list_str.="<option value=\"0\" $selected_str >NONE";
		$cat_list_str.="</select>";

		$tmp=print_r($cat_list,1);
		$cat_list_str.="<!-- $tmp -->";

		$cat_list_str=iconv("gbk","utf-8", $cat_list_str);
		

	}
	else{
		$cat_list_str.="<b>Save your blog first please.</b>";
	}
?>
<fieldset id="hot_topics" class="dbx-box">
	<h3 class="dbx-handle"><?php _e('HOT Topic', 'wp-hot'); ?></h3> 
	<div class="dbx-content">
		<?php echo $old_hot_cat; ?>
		<?php echo $cat_list_str;?>
	</div>
</fieldset>
<?php
}

?>
